package com.css.apps.base.common.sync.service;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;

import com.css.apps.base.common.sync.client.SyncClient;
import com.css.apps.base.common.sync.client.service.SyncService;
import com.css.apps.base.common.sync.model.SyncConstant;
import com.css.apps.base.common.sync.model.SyncOrg;
import com.css.apps.base.common.sync.model.SyncResult;
import com.css.apps.base.common.sync.model.SyncUser;
import com.css.apps.base.org.model.SOrg;
import com.css.apps.base.user.model.SUser;
import com.css.db.query.QueryCache;
import com.css.db.util.StringHelper;
import com.css.util.ListUtil;

public class SyncHelper {
	
	private static SyncClient client = new SyncClient();

	public List<SyncResult> syncUser(SUser item) {
		List<SyncResult> resList = null;
		List<SyncService> syncServices = client.getServices("sync1");
		if (!ListUtil.isLstEmpty(syncServices)) {
			resList = new ArrayList<>();
			SyncUser su = new SyncUser();
			try {
				BeanUtils.copyProperties(su, item);
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
			for (SyncService service : syncServices) {
				SyncResult res = service.syncUser(su);
				System.out.println(res.getResMsg());
				SyncHelper.fillSyncResultUser(res, item);
				resList.add(res);
			}
		}
		return resList;
	}
	
	public List<SyncResult> syncOrg(SOrg item) {
		List<SyncResult> resList = null;
		List<SyncService> syncServices = client.getServices("sync1");
		if (!ListUtil.isLstEmpty(syncServices)) {
			resList = new ArrayList<>();
			SyncOrg so = new SyncOrg();
			try {
				BeanUtils.copyProperties(so, item);
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
			for (SyncService service : syncServices) {
				SyncResult res = service.syncOrg(so);
				System.out.println(res.getResMsg());
				SyncHelper.fillSyncResultOrg(res, item);
				resList.add(res);
			}
		}
		return resList;
	}
	
	public static void fillSyncResultUser(SyncResult rs, SUser user) {
		fillSyncResult(rs, SyncConstant.TYPE_USER, user.getUuid(), user.getRealName());
	}

	public static void fillSyncResultOrg(SyncResult rs, SOrg org) {
		fillSyncResult(rs, SyncConstant.TYPE_ORG, org.getUuid(), org.getName());
	}
	
	public static void fillSyncResult(SyncResult rs, int entityType, String entityId, String entityName) {
		if(StringHelper.isEmpty(entityId)) {
			throw new IllegalArgumentException("entityId can't be empty");
		}
		SyncResult record = QueryCache.get(SyncResult.class, entityId);
		Calendar c = Calendar.getInstance();
		if(record == null) {
			rs.setCreatedTime(c.getTime());
			rs.setFailCount(0);
			rs.setOptType(SyncConstant.OPT_ADD);
		} else {
			rs.setFailCount(rs.getFailCount() + 1);
			rs.setUpdTime(c.getTime());
			rs.setOptType(SyncConstant.OPT_UPD);
		}
		rs.setEntityType(entityType);
		rs.setEntityId(entityId);
		rs.setEntityName(entityName);
	}
	
}
